package generator.service.impl;

import com.atkt.ktkt.model.vod.Course;
import com.atkt.ktkt.model.vod.CourseDescription;
import com.atkt.ktkt.model.vod.Subject;
import com.atkt.ktkt.model.vod.Teacher;
import com.atkt.ktkt.vo.vod.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import generator.service.*;
import generator.mapper.CourseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 罗雅文
 * @description 针对表【course(课程)】的数据库操作Service实现
 * @createDate 2022-09-18 17:24:11
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
        implements CourseService {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseDescriptionService descriptionService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private  ChapterService chapterService;

    @Override
    public Map<String, Object> findPageCourse(Page<Course> coursePage, CourseQueryVo courseQueryVo) {

        //获取条件值
        Long subjectId = courseQueryVo.getSubjectId(); //二层分类
        Long subjectParentId = courseQueryVo.getSubjectParentId(); //一层分类
        String title = courseQueryVo.getTitle();
        Long teacherId = courseQueryVo.getTeacherId();
        //判断条件为空 封装条件
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq(Course::getSubjectId, subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq(Course::getSubjectParentId, subjectParentId);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            wrapper.eq(Course::getTeacherId, teacherId);
        }
        if (!StringUtils.isEmpty(title)) {
            wrapper.like(Course::getTitle, title);
        }

        //调用方法实现条件分页查询
        Page<Course> selectPage = baseMapper.selectPage(coursePage, wrapper);
        List<Course> records = selectPage.getRecords();
        long totalCount = selectPage.getTotal(); //总页数
        long totalPages = selectPage.getPages();  //总记录数

        //查询数据里面有几个id  讲师id 课程分类id (一层二层)
        //获取这些id对面名称 进行封装 最终显示   不显示层级id 显示所对应的内容
        records.stream().forEach(item -> {
            this.getNameById(item);
        });

        //封装数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("totalCount", totalCount);
        map.put("totalPages", totalPages);
        map.put("records", records);

        return map;
    }



    @Override
    public Long saveCourseInfo(CourseFormVo courseFormVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo, course);
        //添加课程的基本信息 course表
        baseMapper.insert(course);

        //添加课程描述信息  course_description
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        //设置课程ID
        courseDescription.setCourseId(course.getId());
        descriptionService.save(courseDescription);

        return course.getId();
    }

    @Override   //根据id 查询课程信息
    public CourseFormVo getCourseFormVoById(Long id) {
        //课程基本信息
        Course course = baseMapper.selectById(id);
        if (course == null) {
            return null;
        }
        //课程描述信息
        CourseDescription courseDescription = descriptionService.getById(id);
        //封装
        CourseFormVo courseFormVo = new CourseFormVo();
        BeanUtils.copyProperties(course, courseFormVo);
        //封装 描述信息
        if (courseDescription != null) {
            courseFormVo.setDescription(courseDescription.getDescription());
        }
        return  courseFormVo;
    }

    @Override  // 修改课程信息
    public void updateCourseId(CourseFormVo courseFormVo) {
        //修改课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);

        //修改课程描述信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescription.setCourseId(course.getId());
        courseDescription.setId(course.getId());
        descriptionService.updateById(courseDescription);
    }

    //根据课程id 查询发布课程信息
    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

    //课程最终发布
    @Override
    public void getPublishCourse(Long id) {
        Course course = baseMapper.selectById(id);
        course.setStatus(1); //已经发布
        course.setPublishTime(new Date());
        baseMapper.updateById(course);
    }

    //删除课程
    @Override
    public void removeCourseId(Long id) {
        //根据课程id 删除小节
        videoService.removeVideoByCourseId(id);

        //根据课程id 删除章节
        chapterService.removeChapterByCourseId(id);

        //根据课程id 课程描述
        descriptionService.removeById(id);

        //根据课程id 删除课程
        baseMapper.deleteById(id);
    }

    //课程列表
    @Override
    public Map<String,Object> findPage(Page<Course> pageParam, CourseQueryVo courseQueryVo) {
        //获取条件值
        String title = courseQueryVo.getTitle();//名称
        Long subjectId = courseQueryVo.getSubjectId();//二级分类
        Long subjectParentId = courseQueryVo.getSubjectParentId();//一级分类
        Long teacherId = courseQueryVo.getTeacherId();//讲师
        //封装条件
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)) {
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id",subjectId);
        }
        if(!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(teacherId)) {
            wrapper.eq("teacher_id",teacherId);
        }
        //调用方法查询
        Page<Course> pages = baseMapper.selectPage(pageParam, wrapper);

        long totalCount = pages.getTotal();//总记录数
        long totalPage = pages.getPages();//总页数
        long currentPage = pages.getCurrent();//当前页
        long size = pages.getSize();//每页记录数
        //每页数据集合
        List<Course> records = pages.getRecords();
        records.stream().forEach(item -> {
            this.getTeacherOrSubjectName(item);
        });

        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);
        map.put("records",records);

        return map;
    }

    //获取讲师和分类名称
    private Course getTeacherOrSubjectName(Course course) {
        Teacher teacher = teacherService.getById(course.getTeacherId());
        if(teacher != null) {
            course.getParam().put("teacherName",teacher.getName());
        }

        Subject subjectOne = subjectService.getById(course.getSubjectParentId());
        if(subjectOne != null) {
            course.getParam().put("subjectParentTitle",subjectOne.getTitle());
        }
        Subject subjectTwo = subjectService.getById(course.getSubjectId());
        if(subjectTwo != null) {
            course.getParam().put("subjectTitle",subjectTwo.getTitle());
        }
        return course;
    }

    //根据id查询课程
    @Override
    public Map<String, Object> getInfoById(Long id) {
        //更新流量量
        Course course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);

        Map<String, Object> map = new HashMap<>();
        CourseVo courseVo = baseMapper.selectCourseVoById(id);
        List<ChapterVo> chapterVoList = chapterService.getNestedTreeList(id);
        CourseDescription courseDescription = descriptionService.getById(id);
        Teacher teacher = teacherService.getById(course.getTeacherId());

        //TODO后续完善
        Boolean isBuy = false;

        map.put("courseVo", courseVo);
        map.put("chapterVoList", chapterVoList);
        map.put("description", null != courseDescription ?
                courseDescription.getDescription() : "");
        map.put("teacher", teacher);
        map.put("isBuy", isBuy);//是否购买
        return map;
    }


    private Course getNameById(Course item) {
        //根据讲师id 获取讲师
        Teacher teacherById = teacherService.getById(item.getTeacherId());
        if (teacherById != null) {
            String name = teacherById.getName();
            item.getParam().put("teacherName", name);
        }
        //根据课程分类id获取课程分类名称
        Subject subjectByIdOne = subjectService.getById(item.getSubjectParentId());
        if (subjectByIdOne != null) {
            item.getParam().put("subjectParentTitle", subjectByIdOne.getTitle());
        }
        Subject subjectTwo = subjectService.getById(item.getSubjectId());
        if (subjectTwo != null) {
            item.getParam().put("subjectTitle", subjectTwo.getTitle());
        }
        return item;

    }
}




